
import json

import requests
from django.http import JsonResponse
from django.shortcuts import render,redirect,HttpResponse

# Create your views here.
from .import OpenStack_API


def login(request):
    return render(request,'zgh0381/login.html')

#定义登录视图函数
def login_check(request):
    if request.method == "POST":
            domain = request.POST["domain"]
            username = request.POST["username"]
            password = request.POST["password"]
            domain_res = OpenStack_API.get_domain_token(domain,username,password)
            if domain_res.status_code == 201:
                request.session["domain_token"] = domain_res.headers.get("X-Subject-Token")
                project_res = OpenStack_API.get_project_token(request.session.get("domain_token",None))
                request.session["project_token"] = project_res.headers.get("X-Subject-Token")
                return redirect('/zgh0381/project/')

            else:
                return render(request, 'zgh0381/login.html')
    else:
        return render(request,'zgh0381/login.html')

def projectlist(request):
    return render(request, 'zgh0381/projectlist.html')

def userlist(request):
    return render(request, 'zgh0381/userlist.html')
#登出功能视图函数
def loginout(request):
    domain_token = request.session.get("domain_token", None)
    project_token = request.session.get("project_token", None)
    if not (domain_token and project_token):
        return redirect('/zgh0381/login')
    project_list = request.session.get("project_res", None)
    if project_list:
        return render(request, 'zgh0381/project.html')
    else:
        project_tokenresp = OpenStack_API.token_project_del(domain_token,project_token)
        domain_tokenresp = OpenStack_API.token_domain_del(domain_token)
        if project_tokenresp.status_code and domain_tokenresp.status_code == 204:
            return redirect('/zgh0381/login/')
        else:
            return render(request,'zgh0381/project.html')
#定义项目视图函数
def project_list(request):
    domain_token = request.session.get("domain_token",None)
    project_token = request.session.get("project_token",None)
    if not (domain_token and project_token):
        return redirect('/zgh0381/login')
    project_list = request.session.get("project_res",None)
    if project_list:
        return render(request,'zgh0381/project.html')
    else:
        resp = OpenStack_API.projects_list(project_token)
        if resp.status_code == 200:
            request.session["project_list"] = resp.json()
            return render(request,'zgh0381/project.html')
        else:
            return redirect('/zgh0381/login/')

def pro_json(request):
    domain_token = request.session.get("domain_token", None)
    project_token = request.session.get("project_token", None)
    resp = OpenStack_API.projects_list(project_token)
    if resp.status_code == 200:
        result = resp.json()['projects']
        for i in result:
            domain_id = i.get('domain_id')
        request.session["domain_id"] = domain_id
        domain_info_list = OpenStack_API.domain_info(domain_token, domain_id)
        domain_info_re = domain_info_list.json().get('domain')
        for j in result:
            j['domain_id'] = domain_info_re.get('name')
        re = {"code": 0, "msg": "success", "count": 100, "data": result}
        # 返回符合layui的json数据
        return HttpResponse(json.dumps(re), content_type="application/json,charset=utf-8")
#删除项目
def project_delete(request,id):
    domain_token = request.session.get("domain_token", None)
    project_token = request.session.get("project_token", None)
    if not (domain_token and project_token):
        return redirect('/zgh0381/login.html')
    else:
        respones = OpenStack_API.projects_del(domain_token,id)
        if respones.status_code == 204:
            return JsonResponse({"res": 1})
        else:
            return render(request, 'zgh0381/projectlist.html' )
#创建项目
def projects_creates(request):
    return render(request,'zgh0381/projectcreate.html')
def pro_createcheck(request):
    if request.method == "POST":
        projectname = request.POST.get("project_name")
        description = request.POST.get("desc")
        domain_token = request.session.get("domain_token", None)
        domain_res = OpenStack_API.projects_create(domain_token,projectname,description)
        if domain_res.status_code == 201:
            return JsonResponse({"res": 1},safe=False)
        else:
            return JsonResponse({"res": 0},safe=False)
#列举项目详细信息
def project_detail(request):
    return render(request,'zgh0381/projectdetail.html')


def project_dodetail(request,projectid):
    domain_token = request.session.get("domain_token", None)
    project_token = request.session.get("project_token", None)
    if not (domain_token and project_token):
        return redirect('/zgh0381/login')
    project_list = request.session.get("project_res", None)
    if project_list:
        return render(request, 'zgh0381/projectlist.html')
    else:
        resp = OpenStack_API. projects_info(domain_token,projectid)
        if resp.status_code == 200:
            request.session["project_list"] = resp.json()
            return JsonResponse({"res": 1}, safe=False)
        else:
            return redirect('/zgh0381/projectlist/')
#更新项目信息
def project_update(request):
    return render(request,'zgh0381/projectupdate.html')

def project_do_update(request):
    if request.method == "POST":
        projectupdateid = request.POST.get("domain_id")
        projectname = request.POST.get("project_name")
        description = request.POST.get("desc")
        domain_token = request.session.get("domain_token", None)
        domain_res = OpenStack_API.projects_update(domain_token,projectupdateid,description,projectname)
        if domain_res.status_code == 200:
            return JsonResponse({"res": 1}, safe=False)
        else:
            return JsonResponse({"res": 0}, safe=False)
    else:
        return render(request,'zgh0381/projectupdate.html')


def project_update_data(request):
   if request.method == "POST":
       domain_name = request.POST.get("domain")
       project_name = request.POST.get("project_name")
       desc = request.POST.get("desc")
       request.session["domain_name"] = domain_name
       request.session["project_name"] = project_name
       request.session["desc"] = desc
       return render(request,'zgh0381/projectupdate.html')
   else:
      return render(request,'zgh0381/projectlist.html')

#列出用户
def user_json(request):
    domain_token = request.session.get("domain_token",None)
    project_token = request.session.get("project_token",None)
    if not (domain_token and project_token):
        return redirect('/zgh0381/login')
    user_lists = request.session.get("project_res",None)
    if user_lists:
        return render(request,'zgh0381/project.html')
    else:
        resp = OpenStack_API.users_list(project_token)
        if resp.status_code == 200:
            result = resp.json()['users']
            for i in result:
                domain_id = i.get('domain_id')
            request.session["domain_id"] = domain_id
            domain_info_list = OpenStack_API.domain_info(domain_token, domain_id)
            domain_info_re = domain_info_list.json().get('domain')
            for j in result:
                j['domain_id'] = domain_info_re.get('name')
            re = {"code": 0, "msg": "success", "count": 100, "data": result}
            # 返回符合layui的json数据
            return HttpResponse(json.dumps(re), content_type="application/json,charset=utf-8")


#删除用户
def user_delete(request,userid):
    domain_token = request.session.get("domain_token", None)
    project_token = request.session.get("project_token", None)
    if not (domain_token and project_token):
        return redirect('/zgh0381/login.html')
    else:
        res = OpenStack_API.users_del(domain_token,userid)
        if res.status_code == 204:
            return JsonResponse({"res": 1})
        else:
            return JsonResponse({"res": 0})
#添加用户
def user_create(request):
    return render(request,'zgh0381/usercreate.html')

#添加操作
def user_docreate(request):
    if request.method == "POST":
        username = request.POST.get("user_name")
        description = request.POST.get("desc")
        email = request.POST.get("email")
        password = request.POST.get("pwd")
        request.session["old_pwd"] = password
        domain_token = request.session.get("domain_token", None)
        res = OpenStack_API.users_create(domain_token,username,password,description,email)
        if res.status_code == 201:
            return JsonResponse({"res": 1},safe=False)
        else:
            return JsonResponse({"res": 0},safe=False)
#查看用户详细信息
def user_detail(request):
   return render(request,'zgh0381/userinfo.html')

def user_dodetail(request,userid):
    domain_token = request.session.get("domain_token", None)
    project_token = request.session.get("project_token", None)
    if not (domain_token and project_token):
        return redirect('/zgh0381/login')
    project_list = request.session.get("project_res", None)
    if project_list:
        return render(request, 'zgh0381/projectlist.html')
    else:
        resp = OpenStack_API.users_info(domain_token,userid)
        if resp.status_code == 200:
            request.session["user_list"] = resp.json()
            return JsonResponse({"res": 1},safe=False)
        else:
            return JsonResponse({"res": 0},safe=False)

#编辑用户
def edit_user(request):
    if request.method == "POST":
        user_name = request.POST.get("user_name")
        user_id = request.POST.get("user_id")
        desc = request.POST.get("desc")
        email = request.POST.get("email")
        request.session["user_name1"] = user_name
        request.session["desc"] = desc
        request.session["email1"] = email
        request.session["user_id"] = user_id
        return render(request, 'zgh0381/userupdate.html')
    else:
        return render(request, 'zgh0381/userupdate.html')

def user_update(request):
    if request.method == "POST":
            username = request.POST.get("user_name")
            description = request.POST.get("desc")
            email = request.POST.get("email")
            userid = request.session.get("user_id", None)
            domain_token = request.session.get("domain_token", None)
            res = OpenStack_API.users_update(domain_token,username,description,email,userid)
            if res.status_code == 200:
                return JsonResponse({"res": 1},safe=False)
            else:
                return JsonResponse({"res": 0},safe=False)
    else:
        return render(request, 'zgh0381/userupdate.html')

#修改密码
def change_pwd(request):
    if request.method == "POST":
        username = request.POST.get("user_name")
        userid = request.POST.get("userid")
        request.session["userid"] = userid
        request.session["username"] = username
        return render(request,'zgh0381/userchangepwd.html')
    return render(request, 'zgh0381/userchangepwd.html')

def change_dopwd(request):
    domain_token = request.session.get("domain_token", None)
    user_id = request.session.get("userid", None)
    old_pwd = request.session.get("old_pwd",None)
    pwd = request.POST.get("pwd")
    rep = OpenStack_API.users_changepassword(domain_token,user_id,pwd,old_pwd)
    if rep.status_code == 204:
        return JsonResponse({"res": 1}, safe=False)
    else:
        return JsonResponse({"res": 0}, safe=False)


#列出组
def group_list(request):
    return render(request,'zgh0381/grouplist.html')

def group_json(request):
    domain_token = request.session.get("domain_token", None)
    project_token = request.session.get("project_token", None)
    if not (domain_token and project_token):
        return redirect('/zgh0381/login')
    user_lists = request.session.get("project_res", None)
    if user_lists:
        return render(request, 'zgh0381/project.html')
    else:
        resp = OpenStack_API.groups_list(project_token)
        if resp.status_code == 200:
            result = resp.json()['groups']
            for i in result:
                domain_id = i.get('domain_id')
            request.session["domain_id"] = domain_id
            domain_info_list = OpenStack_API.domain_info(domain_token, domain_id)
            domain_info_re = domain_info_list.json().get('domain')
            for j in result:
                j['domain_id'] = domain_info_re.get('name')
            re = {"code": 0, "msg": "success", "count": 100, "data": result}
            # 返回符合layui的json数据
            return HttpResponse(json.dumps(re), content_type="application/json,charset=utf-8")

#编辑组
def group_update(request):
    if request.method == "POST":
        groupname = request.POST.get("group_name")
        groupid = request.POST.get("group_id")
        desc = request.POST.get("desc")
        request.session["groupname"] = groupname
        request.session["group_desc"] = desc
        request.session["groupid"] = groupid
        return render(request, 'zgh0381/groupdate.html')
    return render(request,"zgh0381/groupdate.html")


def group_doupdate(request):
    if request.method == "POST":
            groupid = request.session.get("groupid", None)
            domain_token = request.session.get("domain_token", None)
            groupname = request.POST.get("group_name")
            description = request.POST.get("desc")
            res = OpenStack_API.groups_update(domain_token,groupname,description,groupid)
            if res.status_code == 200:

                return JsonResponse({"res": 1}, safe=False)
            else:
                return JsonResponse({"res": 0}, safe=False)
    else:
        return render(request,'zgh0381/groupdate.html')
#添加组
def group_create(request):
    return render(request,"zgh0381/groupcreat.html")

def group_docreate(request):
     if request.method == "POST":
         domain_token = request.session.get("domain_token", None)
         groupname = request.POST.get("group_name")
         description = request.POST.get("desc")
         res = OpenStack_API.groups_create(domain_token, description, groupname)
         if res.status_code == 201:
             return JsonResponse({"res": 1}, safe=False)
         else:
             return JsonResponse({"res": 0}, safe=False)
#删除组
def group_delete(request,groupid):
    domain_token = request.session.get("domain_token", None)
    project_token = request.session.get("project_token", None)
    if not (domain_token and project_token):
        return redirect('/zgh0381/login.html')
    else:
        res = OpenStack_API.groups_del(domain_token,groupid)
        if res.status_code == 204:
            return JsonResponse({"res": 1}, safe=False)
        else:
            return JsonResponse({"res": 0}, safe=False)
#列出组里面的用户
def manage_group(request):
    if request.method == "POST":
        groupname = request.POST.get("group_name")
        groupid = request.POST.get("group_id")
        request.session["group_name"] = groupname
        request.session["group_id"] = groupid
        return render(request, 'zgh0381/groupuser.html')
    return render(request,'zgh0381/groupuser.html')

def group_userjson(request):
    domain_token = request.session.get("domain_token", None)
    project_token = request.session.get("project_token", None)
    groupid = request.session.get("group_id",None)
    if not (domain_token and project_token):
        return redirect('/zgh0381/login')
    project_list = request.session.get("project_res", None)
    if project_list:
        return render(request, 'zgh0381/projectlist.html')
    else:
        resp = OpenStack_API.groups_user(domain_token,groupid)
        if resp.status_code == 200:
            result = resp.json()['users']
            re = {"code": 0, "msg": "success", "count": 100, "data": result}
            # 返回符合layui的json数据
            return HttpResponse(json.dumps(re), content_type="application/json,charset=utf-8")
#将用户添加到组
def group_add(request):
    return render(request,'zgh0381/groupuserlist.html')

def group_addjson(request):
    domain_token = request.session.get("domain_token", None)
    project_token = request.session.get("project_token", None)
    if not (domain_token and project_token):
        return redirect('/zgh0381/login')
    user_lists = request.session.get("project_res", None)
    if user_lists:
        return render(request, 'zgh0381/project.html')
    else:
        resp = OpenStack_API.users_list(project_token)
        if resp.status_code == 200:
            result = resp.json()['users']
            for i in result:
                domain_id = i.get('domain_id')
            request.session["domain_id"] = domain_id
            domain_info_list = OpenStack_API.domain_info(domain_token, domain_id)
            domain_info_re = domain_info_list.json().get('domain')
            for j in result:
                j['domain_id'] = domain_info_re.get('name')
            re = {"code": 0, "msg": "success", "count": 100, "data": result}
            # 返回符合layui的json数据
            return HttpResponse(json.dumps(re), content_type="application/json,charset=utf-8")
def group_users(request):
    if request.method == "POST":
        domain_token = request.session.get("domain_token", None)
        project_token = request.session.get("project_token", None)
        groupid = request.session.get("group_id",None)
        userinfo = request.POST.get("user_info")
        userjson = json.loads(userinfo)
        user_id = userjson[0]["id"]
        if not (domain_token and project_token):
            return redirect('/zgh0381/login')
        user_lists = request.session.get("project_res", None)
        if user_lists:
            return render(request, 'zgh0381/project.html')
        else:
            resp = OpenStack_API.users_add_group(domain_token,groupid,user_id)
            if resp.status_code == 204:
                return JsonResponse({"res": 1}, safe=False)
            else:
                return JsonResponse({"res": 0}, safe=False)
#删除组里面的用户
def group_deleteuser(request):
    if request.method == "POST":
        domain_token = request.session.get("domain_token", None)
        project_token = request.session.get("project_token", None)
        groupid = request.session.get("group_id",None)
        userinfo = request.POST.get("user_info")
        userjson = json.loads(userinfo)
        user_id = userjson[0]["id"]
        if not (domain_token and project_token):
            return redirect('/zgh0381/login')
        user_lists = request.session.get("project_res", None)
        if user_lists:
            return render(request, 'zgh0381/project.html')
        else:
            resp = OpenStack_API.groups_del_user(domain_token,groupid,user_id)
            if resp.status_code == 204:
                return JsonResponse({"res": 1}, safe=False)
            else:
                return JsonResponse({"res": 0}, safe=False)


#列出角色
def role_list(request):
    return render(request,"zgh0381/roleslist.html")

def role_json(request):
    domain_token = request.session.get("domain_token", None)
    project_token = request.session.get("project_token", None)
    if not (domain_token and project_token):
        return redirect('/zgh0381/login')
    user_lists = request.session.get("project_res", None)
    if user_lists:
        return render(request, 'zgh0381/project.html')
    else:
        resp = OpenStack_API.roles_list(project_token)
        if resp.status_code == 200:
            result = resp.json()['roles']
            re = {"code": 0, "msg": "success", "count": 100, "data": result}
            # 返回符合layui的json数据
            return HttpResponse(json.dumps(re), content_type="application/json,charset=utf-8")
#添加角色
def role_create(request):
    return render(request,'zgh0381/rolecreate.html')

def role_docreate(request):
    if request.method == "POST":
        domain_token = request.session.get("domain_token", None)
        rolename = request.POST.get("role_name")
        description = request.POST.get("desc")
        res = OpenStack_API.roles_create(domain_token, description, rolename)
        if res.status_code == 201:
            return JsonResponse({"res": 1}, safe=False)
        else:
            return JsonResponse({"res": 0}, safe=False)
#删除角色
def role_delete(request,id):
    domain_token = request.session.get("domain_token", None)
    project_token = request.session.get("project_token", None)
    if not (domain_token and project_token):
        return redirect('/zgh0381/login.html')
    else:
        resp = OpenStack_API.roles_del(domain_token,id)
        if resp.status_code == 204:
            return JsonResponse({"res": 1}, safe=False)
        else:
            return JsonResponse({"res": 0}, safe=False)
#编辑角色
def role_update(request):
        if request.method == "POST":
            rolename = request.POST.get("role_name")
            roleid = request.POST.get("role_id")
            request.session["rolename"] = rolename
            request.session["roleid"] = roleid
            return render(request, 'zgh0381/roleupdate.html')
        return render(request, 'zgh0381/roleupdate.html')

def role_doupdate(request):
    domain_token = request.session.get("domai   n_token", None)
    id = request.session.get("roleid", None)
    if request.method == "POST":
        rolename = request.POST.get("role_name")
        description = request.POST.get("desc")
        res = OpenStack_API.roles_update(domain_token, description, rolename, id)
        if res.status_code == 200:
            return JsonResponse({"res": 1}, safe=False)
        else:
            return JsonResponse({"res": 0}, safe=False)



def Summary(request):
    return render(request,'zgh0381/Summary.html')


#列出flavor
def flavors(request):
    return render(request,'zgh0381/flavorlist.html')

def flavors_listjson(request):
    domain_token = request.session.get("domain_token", None)
    project_token = request.session.get("project_token", None)
    if not (domain_token and project_token):
        return redirect('/zgh0381/login')
    user_lists = request.session.get("project_res", None)
    if user_lists:
        return render(request, 'zgh0381/project.html')
    else:
        resp = OpenStack_API.flavor_list(project_token)
        if resp.status_code == 200:
            result = resp.json()['flavors']
            re = {"code": 0, "msg": "success", "count": 100, "data": result}
            # 返回符合layui的json数据
            return HttpResponse(json.dumps(re), content_type="application/json,charset=utf-8")
# 查看flavor详细信息
def flavor_page(request):
    return render(request,'zgh0381/flavorinfo.html')


def flavors_detail(request,id):
    domain_token = request.session.get("domain_token", None)
    project_token = request.session.get("project_token", None)
    if not (domain_token and project_token):
        return redirect('/zgh0381/login')
    project_list = request.session.get("project_res", None)
    if project_list:
        return render(request, 'zgh0381/projectlist.html')
    else:
        resp = OpenStack_API.flavor_info(project_token,id)
        if resp.status_code == 200:
            request.session["flavors_detail"] = resp.json()
            return JsonResponse({"res": 1}, safe=False)
        else:
            return JsonResponse({"res": 0}, safe=False)
# 列举server
def server_list(request):
    return render(request,'zgh0381/serverlist.html')

def serverlist_json(request):
    domain_token = request.session.get("domain_token", None)
    project_token = request.session.get("project_token", None)
    if not (domain_token and project_token):
        return redirect('/zgh0381/login')
    user_lists = request.session.get("project_res", None)
    if user_lists:
        return render(request, 'zgh0381/project.html')
    else:
        resp = OpenStack_API.servers_list(project_token)
        if resp.status_code == 200:
            result = resp.json()['servers']
            re = {"code": 0, "msg": "success", "count": 100, "data": result}
            # 返回符合layui的json数据
            return HttpResponse(json.dumps(re), content_type="application/json,charset=utf-8")
# 列出可用image
def image_list(request):
    return render(request,'zgh0381/imageslist.html')


def imagelist_json(request):
    domain_token = request.session.get("domain_token", None)
    project_token = request.session.get("project_token", None)
    if not (domain_token and project_token):
        return redirect('/zgh0381/login')
    user_lists = request.session.get("project_res", None)
    if user_lists:
        return render(request, 'zgh0381/project.html')
    else:
        resp = OpenStack_API.images_list(project_token)
        if resp.status_code == 200:
            result = resp.json()['images']
            re = {"code": 0, "msg": "success", "count": 100, "data": result}
            # 返回符合layui的json数据
            return HttpResponse(json.dumps(re), content_type="application/json,charset=utf-8")
# 创建实例
def instance_create(request):
    return render(request,'zgh0381/servercreate.html')

def select_flavor(request):
    if request.method == "POST":
        flavor_info = request.POST.get("flavor_info")
        flavor_json = json.loads(flavor_info)
        flavor_id = flavor_json[0]["id"]
        request.session['flavor_id'] = flavor_id
        return JsonResponse({"res": 1}, safe=False)
    return render(request, 'zgh0381/select_flavor.html')


def select_images(request):
    if request.method == "POST":
        image_info = request.POST.get("image_info")
        image_json = json.loads(image_info)
        image_id = image_json[0]["id"]
        request.session['selectimage_id'] = image_id
        return JsonResponse({"res": 1}, safe=False)
    return render(request, 'zgh0381/select_image.html')

def instance_name(request):
    if request.method == "POST":
        instance_name = request.POST.get("instance_name")
        request.session['instance_name'] = instance_name
        return JsonResponse({"res": 1}, safe=False)
    return render(request,'zgh0381/instance_name.html')


def server_create(request):
    if request.method == "POST":
        servername = request.session.get("instance_name", None)
        flavorid = request.session.get("flavor_id", None)
        imageid = request.session.get("selectimage_id", None)
        project_token = request.session.get("project_token", None)
        res = OpenStack_API.servers_create(project_token,servername,imageid,flavorid)
        if res.status_code == 202:
            return JsonResponse({"res": 1}, safe=False)
        else:
            return JsonResponse({"res": 0}, safe=False)
# 删除实例
def server_delete(request,serverid):
    domain_token = request.session.get("domain_token", None)
    project_token = request.session.get("project_token", None)
    if not (domain_token and project_token):
        return redirect('/zgh0381/login.html')
    else:
        resp = OpenStack_API.servers_del(project_token,serverid)
        if resp.status_code == 204:
            return JsonResponse({"res": 1}, safe=False)
        else:
            return JsonResponse({"res": 0}, safe=False)
# 显示实例详细信息
def instance_detail(request):
    return render(request, 'zgh0381/serverinfo.html')



def server_detail(request,serverid):
    domain_token = request.session.get("domain_token", None)
    project_token = request.session.get("project_token", None)
    if not (domain_token and project_token):
        return redirect('/zgh0381/login')
    project_list = request.session.get("project_res", None)
    if project_list:
        return render(request, 'zgh0381/projectlist.html')
    else:
        resp = OpenStack_API.servers_info(project_token,serverid)
        if resp.status_code == 200:
            request.session["servers_detail"] = resp.json()
            return JsonResponse({"res": 1}, safe=False)
        else:
            return JsonResponse({"res": 0}, safe=False)

# 启动实例
def server_on(request,serverid):
        domain_token = request.session.get("domain_token", None)
        project_token = request.session.get("project_token", None)
        if not (domain_token and project_token):
            return redirect('/zgh0381/login.html')
        else:
            resp = OpenStack_API.servers_start(project_token,serverid)
            if resp.status_code == 202:
                return JsonResponse({"res": 1}, safe=False)
            else:
                return JsonResponse({"res": 0}, safe=False)

# 重启实例
def server_restart(request,serverid):
        domain_token = request.session.get("domain_token", None)
        project_token = request.session.get("project_token", None)
        if not (domain_token and project_token):
            return redirect('/zgh0381/login.html')
        else:
            resp = OpenStack_API.servers_reboot(project_token,serverid)
            if resp.status_code == 202:
                return JsonResponse({"res": 1}, safe=False)
            else:
                return JsonResponse({"res": 0}, safe=False)
# 暂停实例
def server_stop(request,serverid):
    domain_token = request.session.get("domain_token", None)
    project_token = request.session.get("project_token", None)
    if not (domain_token and project_token):
        return redirect('/zgh0381/login.html')
    else:
        resp = OpenStack_API.servers_pause(project_token,serverid)
        if resp.status_code == 202:
            return JsonResponse({"res": 1}, safe=False)
        else:
            return JsonResponse({"res": 0}, safe=False)
# 取消暂停server
def server_unstop(request,serverid):
    domain_token = request.session.get("domain_token", None)
    project_token = request.session.get("project_token", None)
    if not (domain_token and project_token):
        return redirect('/zgh0381/login.html')
    else:
        resp = OpenStack_API.servers_unpause(project_token,serverid)
        if resp.status_code == 202:
            return JsonResponse({"res": 1}, safe=False)
        else:
            return JsonResponse({"res": 0}, safe=False)
#  挂起servers
def server_hang(request,serverid):
    domain_token = request.session.get("domain_token", None)
    project_token = request.session.get("project_token", None)
    if not (domain_token and project_token):
        return redirect('/zgh0381/login.html')
    else:
        resp = OpenStack_API.servers_suspend(project_token,serverid)
        if resp.status_code == 202:
            return JsonResponse({"res": 1}, safe=False)
        else:
            return JsonResponse({"res": 0}, safe=False)
#  恢复挂起的servers
def server_rehang(request,serverid):
    domain_token = request.session.get("domain_token", None)
    project_token = request.session.get("project_token", None)
    if not (domain_token and project_token):
        return redirect('/zgh0381/login.html')
    else:
        resp = OpenStack_API.servers_resume_suspended(project_token,serverid)
        if resp.status_code == 202:
            return JsonResponse({"res": 1}, safe=False)
        else:
            return JsonResponse({"res": 0}, safe=False)
#   创建快照
def shot_create(request):
    if request.method == "POST":
            serverid = request.POST.get("server_id")
            shotname = request.POST.get("shotname")
            project_token = request.session.get("project_token", None)
            res = OpenStack_API.servers_snapshot(project_token,serverid,shotname)
            if res.status_code == 202:
                return JsonResponse({"res": 1}, safe=False)
            else:
                return JsonResponse({"res": 0}, safe=False)
# 关闭实例
def server_off(request,serverid):
    domain_token = request.session.get("domain_token", None)
    project_token = request.session.get("project_token", None)
    if not (domain_token and project_token):
        return redirect('/zgh0381/login.html')
    else:
        resp = OpenStack_API.servers_stop(project_token,serverid)
        if resp.status_code == 202:
            return JsonResponse({"res": 1}, safe=False)
        else:
            return JsonResponse({"res": 0}, safe=False)
# 列举项目映像
def image_lists(request):
    domain_token = request.session.get("domain_token", None)
    project_token = request.session.get("project_token", None)
    if not (domain_token and project_token):
        return redirect('/zgh0381/login')
    user_lists = request.session.get("project_res", None)
    if user_lists:
        return render(request, 'zgh0381/project.html')
    else:
        resp = OpenStack_API.images_list(project_token)
        if resp.status_code == 200:
            request.session["images_list"] = resp.json()
            return render(request, 'zgh0381/imageslist.html')
        else:
            return redirect('/zgh0381/login/')
# 创建镜像
def imagecreate(request):
    return render(request,'zgh0381/imagecreate.html')


def image_creates(request):
    if request.method == "POST":
            imagename = request.POST.get("image_name")
            project_token = request.session.get("project_token", None)
            description = request.POST.get("desc")
            container_format = request.POST.get("container_format")
            disk_format = request.POST.get("disk_name")
            res = OpenStack_API.image_create(imagename,container_format,disk_format,description,project_token)
            if res.status_code == 201:
                return JsonResponse({"res": 1}, safe=False)
            else:
                return JsonResponse({"res": 0}, safe=False)
# 上传镜像
def upload_file(request):
        if request.method == "POST":
            image_id = request.POST.get("image_id")
            request.session["image_id"] = image_id
            return render(request, 'zgh0381/filecreate.html')
        return render(request,'zgh0381/filecreate.html')


def image_file(request):
    if request.method == "POST":
            imageid = request.session.get("image_id", None)
            path = request.POST.get('path')
            print(path)
            project_token = request.session.get("project_token", None)
            res = OpenStack_API.image_upload(path,project_token,imageid)
            if res.status_code == 204:
                return JsonResponse({"res": 1}, safe=False)
            else:
                return JsonResponse({"res": 0}, safe=False)
# 查看镜像详细信息
def imagesDtail(request):
    return render(request,'zgh0381/imagelist.html')


def image_detail(request,imageid):
    domain_token = request.session.get("domain_token", None)
    project_token = request.session.get("project_token", None)
    if not (domain_token and project_token):
        return redirect('/zgh0381/login')
    user_lists = request.session.get("project_res", None)
    if user_lists:
        return render(request, 'zgh0381/project.html')
    else:
        resp = OpenStack_API.images_info(imageid,project_token)
        if resp.status_code == 200:
            request.session["image_info"] = resp.json()
            return JsonResponse({"res": 1}, safe=False)
        else:
            return JsonResponse({"res": 0}, safe=False)
# 删除映像
def image_delete(request,imageid):
    domain_token = request.session.get("domain_token", None)
    project_token = request.session.get("project_token", None)
    if not (domain_token and project_token):
        return redirect('/zgh0381/login.html')
    else:
        resp = OpenStack_API.images_del(imageid,project_token)
        if resp.status_code == 204:
            return JsonResponse({"res": 1}, safe=False)
        else:
            return JsonResponse({"res": 0}, safe=False)